﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Globalization;

namespace Light.Utility
{
    public class Localize
    {

        private static Dictionary<string, CPair> _currencyTable;
        private static Dictionary<int, string> _upperNumberTable;

        public static readonly string CURRENCY_DEFAULT = "CNY";


        static Localize()
        {
            if (_currencyTable == null)
            {
                lock (typeof(Localize))
                {
                    if (_currencyTable == null)
                    {
                        _currencyTable = new Dictionary<string, CPair>(50);
                        _currencyTable.Add("CNY", new CPair(0x804, "人民币"));
                        _currencyTable.Add("GBP", new CPair(0x809, "英镑"));
                        _currencyTable.Add("HKD", new CPair(0xc04, "港币"));
                        _currencyTable.Add("USD", new CPair(0x409, "美元"));
                        _currencyTable.Add("CHF", new CPair(0x100c, "瑞士法郎"));
                        _currencyTable.Add("DEM", new CPair(0x407, "德国马克"));
                        _currencyTable.Add("FRF", new CPair(0x40c, "法国法郎"));
                        _currencyTable.Add("SGD", new CPair(0x1004, "新加坡元"));
                        _currencyTable.Add("BEF", new CPair(0x80c, "比利时法郎"));
                        _currencyTable.Add("NLG", new CPair(0x413, "荷兰盾"));
                        _currencyTable.Add("SEK", new CPair(0x41d, "瑞典克郎"));
                        _currencyTable.Add("DKK", new CPair(0x406, "丹麦克郎"));
                        _currencyTable.Add("NOK", new CPair(0x414, "挪威克郎"));
                        _currencyTable.Add("ATS", new CPair(0xc07, "奥地利先令"));
                        _currencyTable.Add("ITL", new CPair(0x410, "意大利里拉"));
                        _currencyTable.Add("JPY", new CPair(0x411, "日元"));
                        _currencyTable.Add("CAD", new CPair(0x1009, "加元"));
                        _currencyTable.Add("AUD", new CPair(0xc09, "澳元"));
                        _currencyTable.Add("TZS", new CPair(0, "坦桑尼亚先令"));
                        _currencyTable.Add("ESP", new CPair(0xc0a, "西班牙比塞塔"));
                        _currencyTable.Add("MYR", new CPair(0x43e, "马来西亚林吉特"));
                        _currencyTable.Add("XEL", new CPair(0x407, "欧洲货币单位"));
                        _currencyTable.Add("LKR", new CPair(0, "斯里兰卡卢比"));
                        _currencyTable.Add("DZD", new CPair(0x1401, "阿尔及利亚第纳尔"));
                        _currencyTable.Add("FIM", new CPair(0x40b, "芬兰马克"));
                        _currencyTable.Add("GHC", new CPair(0, "加纳塞地"));
                        _currencyTable.Add("IQD", new CPair(0x801, "伊拉克地那尔"));
                        _currencyTable.Add("MLF", new CPair(0, "马里法郎"));
                        _currencyTable.Add("MAD", new CPair(0x1801, "摩洛哥地拉姆"));
                        _currencyTable.Add("SLL", new CPair(0, "塞拉利昂"));
                        _currencyTable.Add("IRR", new CPair(0x429, "伊朗里亚尔"));
                        _currencyTable.Add("NPR", new CPair(0, "尼泊尔卢比"));
                        _currencyTable.Add("ALL", new CPair(0x41c, "阿尔巴尼亚列克"));
                        _currencyTable.Add("ROL", new CPair(0x418, "罗马尼亚列依"));
                        _currencyTable.Add("KPW", new CPair(0x412, "朝鲜币"));
                        _currencyTable.Add("VND", new CPair(0x42a, "越南盾"));
                        _currencyTable.Add("UUP", new CPair(0x40e, "匈牙利福林"));
                        _currencyTable.Add("BGL", new CPair(0x402, "保加利亚列瓦"));
                        _currencyTable.Add("CSK", new CPair(0x405, "捷克克郎"));
                        _currencyTable.Add("PLZ", new CPair(0x415, "波兰兹罗提"));
                        _currencyTable.Add("BUR", new CPair(0x419, "俄罗斯卢布"));
                        _currencyTable.Add("FUG", new CPair(0x450, "蒙古图格里克"));
                        _currencyTable.Add("ZMR", new CPair(0, "赞比亚克瓦查"));
                        _currencyTable.Add("MOP", new CPair(0x1404, "澳门币"));
                        _currencyTable.Add("PHP", new CPair(0x3409, "菲律宾比索"));
                        _currencyTable.Add("BUK", new CPair(0, "缅甸币"));
                        _currencyTable.Add("THB", new CPair(0x41e, "泰币"));
                        _currencyTable.Add("INR", new CPair(0x44f, "印度罗比"));
                        _currencyTable.Add("MTP", new CPair(0, "马尔他镑"));
                        _currencyTable.Add("NZD", new CPair(0x1409, "新西兰元"));
                    }
                    if (_upperNumberTable == null)
                    {
                        _upperNumberTable = new Dictionary<int, string>();
                        _upperNumberTable.Add(0, "零");
                        _upperNumberTable.Add(1, "一");
                        _upperNumberTable.Add(2, "两");
                        _upperNumberTable.Add(3, "三");
                        _upperNumberTable.Add(4, "四");
                        _upperNumberTable.Add(5, "五");
                        _upperNumberTable.Add(6, "六");
                        _upperNumberTable.Add(7, "七");
                        _upperNumberTable.Add(8, "八");
                        _upperNumberTable.Add(9, "九");
                    }
                }
            }
            int lCID = CultureInfo.CurrentCulture.LCID;
            foreach (string str in _currencyTable.Keys)
            {
                CPair pair = _currencyTable[str];
                if (pair.Cid == lCID)
                {
                    CURRENCY_DEFAULT = str;
                    break;
                }
            }
        }

        public static CultureInfo GetCultureInfo()
        {
            return GetCultureInfo(null);
        }
        public static string ToUpperNumber(int number)
        {
            if (_upperNumberTable.ContainsKey(number))
                return _upperNumberTable[number];
            return "";
        }

        public static CultureInfo GetCultureInfo(string currency)
        {
            int cid;
            if (string.IsNullOrEmpty(currency))
            {
                currency = CURRENCY_DEFAULT;
            }
            currency = currency.ToUpper();
            if (_currencyTable.ContainsKey(currency))
            {
                CPair pair = _currencyTable[currency];
                cid = pair.Cid;
            }
            else
            {
                cid = CultureInfo.CurrentCulture.LCID;
            }
            return new CultureInfo(cid);
        }

        public static string GetCurrencyName(string currency)
        {
            if (string.IsNullOrEmpty(currency))
            {
                currency = CURRENCY_DEFAULT;
            }
            if (_currencyTable.ContainsKey(currency))
            {
                CPair pair = _currencyTable[currency];
                return pair.CurrencyName;
            }
            CultureInfo cultureInfo = GetCultureInfo(currency);
            return currency.ToUpper(cultureInfo);
        }

        public static DateTimeFormatInfo GetDateTimeFormatInfo()
        {
            return GetDateTimeFormatInfo(CURRENCY_DEFAULT);
        }

        public static DateTimeFormatInfo GetDateTimeFormatInfo(string currency)
        {
            CultureInfo cultureInfo = GetCultureInfo(currency);
            if (cultureInfo != null)
            {
                return cultureInfo.DateTimeFormat;
            }
            return DateTimeFormatInfo.InvariantInfo;
        }

        public static NumberFormatInfo GetNumberFormatInfo()
        {
            return GetNumberFormatInfo(CURRENCY_DEFAULT);
        }

        public static NumberFormatInfo GetNumberFormatInfo(string currency)
        {
            CultureInfo cultureInfo = GetCultureInfo(currency);
            if (cultureInfo != null)
            {
                return cultureInfo.NumberFormat;
            }
            return NumberFormatInfo.InvariantInfo;
        }

        public static decimal ParseCurrencyString(string text)
        {
            return ParseCurrencyString(CURRENCY_DEFAULT, text);
        }

        public static decimal ParseCurrencyString(string currency, string text)
        {
            return decimal.Parse(text, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign, GetNumberFormatInfo(currency));
        }

        public static string ToChineseCurrency(decimal number)
        {
            string[] strArray = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            string[] strArray2 = new string[] { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆" };
            number = decimal.Round(number, 2);
            number = Math.Abs(number);
            if (number == 0M)
            {
                return "零元";
            }
            string str = number.ToString("F");
            string str2 = string.Empty;
            string str3 = string.Empty;
            int index = str.IndexOf(".");
            str2 = str.Substring(0, index);
            str3 = str.Substring(index + 1);
            StringBuilder builder = new StringBuilder();
            int length = str2.Length;
            int num3 = 1;
            while (num3 <= length)
            {
                string s = str2.Substring(length - num3, 1);
                builder.Insert(0, strArray[int.Parse(s)] + strArray2[num3 + 1]);
                num3++;
            }
            builder.Replace("零拾", "零");
            builder.Replace("零佰", "零");
            builder.Replace("零仟", "零");
            builder.Replace("零元", "元");
            do
            {
                builder.Replace("零零", "零");
                num3 = builder.ToString().IndexOf("零零");
            }
            while (num3 != -1);
            builder.Replace("拾零万", "拾万");
            builder.Replace("佰零万", "佰万");
            builder.Replace("仟零万", "仟万");
            builder.Replace("亿零万", "亿");
            builder.Replace("拾零亿", "拾亿");
            builder.Replace("佰零亿", "佰亿");
            builder.Replace("仟零亿", "仟亿");
            builder.Replace("兆零亿", "兆");
            do
            {
                builder.Replace("零零", "零");
                num3 = builder.ToString().IndexOf("零零");
            }
            while (num3 != -1);
            builder.Replace("零元", "元");
            str2 = builder.ToString();
            if (str2 == "元")
            {
                str2 = string.Empty;
            }
            if (str3 == "00")
            {
                if (str2 == string.Empty)
                {
                    return string.Empty;
                }
                return builder.Append("整").ToString();
            }
            builder = new StringBuilder();
            length = str3.Length;
            for (num3 = 1; num3 <= 2; num3++)
            {
                string str5 = str3.Substring(length - num3, 1);
                builder.Insert(0, strArray[int.Parse(str5)] + strArray2[num3 - 1]);
            }
            if (str2 == string.Empty)
            {
                builder.Replace("零角", string.Empty);
            }
            else
            {
                builder.Replace("零角", "零");
            }
            builder.Replace("零分", string.Empty);
            builder.Insert(0, str2);
            return builder.ToString();
        }

        public static string ToChineseCurrency(decimal? number)
        {
            if (number.HasValue)
            {
                return ToChineseCurrency(number.Value);
            }
            return string.Empty;
        }

        public static string ToCurrencyString(decimal value)
        {
            return ToCurrencyString(CURRENCY_DEFAULT, value);
        }

        public static string ToCurrencyString(decimal? value)
        {
            if (value.HasValue)
            {
                return ToCurrencyString(value.Value);
            }
            return string.Empty;
        }

        public static string ToCurrencyString(string currency, decimal value)
        {
            NumberFormatInfo numberFormatInfo = GetNumberFormatInfo(currency);
            return value.ToString("c", numberFormatInfo);
        }

        public static string ToCurrencyString(string currency, decimal? value)
        {
            if (value.HasValue)
            {
                return ToCurrencyString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToDateTimeString(DateTime value)
        {
            return ToDateTimeString(CURRENCY_DEFAULT, value);
        }

        public static string ToDateTimeString(DateTime? value)
        {
            if (value.HasValue)
            {
                return ToDateTimeString(value.Value);
            }
            return string.Empty;
        }

        public static string ToDateTimeString(string currency, DateTime? value)
        {
            if (value.HasValue)
            {
                return ToDateTimeString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToDateTimeString(string currency, DateTime value)
        {
            DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormatInfo(currency);
            return value.ToString("f", dateTimeFormatInfo);
        }

        public static string ToMonthDayString(DateTime? value)
        {
            if (value.HasValue)
            {
                return ToMonthDayString(value.Value);
            }
            return string.Empty;
        }

        public static string ToMonthDayString(DateTime value)
        {
            return ToMonthDayString(CURRENCY_DEFAULT, value);
        }

        public static string ToMonthDayString(string currency, DateTime value)
        {
            DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormatInfo(currency);
            return value.ToString("M", dateTimeFormatInfo);
        }

        public static string ToMonthDayString(string currency, DateTime? value)
        {
            if (value.HasValue)
            {
                return ToMonthDayString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToYearMonthDayString(DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearMonthDayString(value.Value);
            }
            return string.Empty;
        }

        public static string ToYearMonthDayString(DateTime value)
        {
            return ToYearMonthDayString(CURRENCY_DEFAULT, value);
        }

        public static string ToYearMonthDayString(string currency, DateTime value)
        {
            DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormatInfo(currency);
            return value.ToString("D", dateTimeFormatInfo);
        }

        public static string ToYearMonthDayString(string currency, DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearMonthDayString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToYearMonthString(DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearMonthString(value.Value);
            }
            return string.Empty;
        }

        public static string ToYearMonthString(DateTime value)
        {
            return ToYearMonthString(CURRENCY_DEFAULT, value);
        }

        public static string ToYearMonthString(string currency, DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearMonthString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToYearMonthString(string currency, DateTime value)
        {
            DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormatInfo(currency);
            return value.ToString("Y", dateTimeFormatInfo);
        }

        public static string ToYearString(DateTime value)
        {
            return ToYearString(CURRENCY_DEFAULT, value);
        }

        public static string ToYearString(DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearString(value.Value);
            }
            return string.Empty;
        }

        public static string ToYearString(string currency, DateTime? value)
        {
            if (value.HasValue)
            {
                return ToYearString(currency, value.Value);
            }
            return string.Empty;
        }

        public static string ToYearString(string currency, DateTime value)
        {
            DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormatInfo(currency);
            return value.ToString("yyyy", dateTimeFormatInfo);
        }


        [StructLayout(LayoutKind.Sequential)]
        private struct CPair
        {
            public int Cid;
            public string CurrencyName;
            public CPair(int cid, string currencyName)
            {
                this.Cid = cid;
                this.CurrencyName = currencyName;
            }
        }
    }
}
